package com.weme.common.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Property
import android.view.View
import android.view.animation.Interpolator
import android.view.animation.LinearInterpolator

import kotlin.math.abs
import kotlin.math.sin

class LightView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    private var shaderWidth = 30
    private var rect = Rect()
    private var paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var interpolator: Interpolator = LinearInterpolator()
    private var duration: Long = 1000
    private var startDelay: Long = 1500
    private var animator: ObjectAnimator? = null
    private var canvasTranslate = Int.MIN_VALUE
    private var shader: Shader? = null

    init {


        paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)
        val colors = intArrayOf(Color.parseColor("#00FFFFFF"), Color.parseColor("#4DFFFFFF"))
       // val position = floatArrayOf(0f, 1f)
        shader = LinearGradient(
            0f,
            0f,
            shaderWidth.toFloat(),
            0f,
            colors,
            null,
            Shader.TileMode.CLAMP
        )
        paint.shader = shader
    }

    fun startLight() {
        this.post {
            if (animator != null) {
                animator!!.cancel()
            }
            val property: Property<LightView?, Int> = object : Property<LightView?, Int>(
                Int::class.java, "canvasTranslate"
            ) {
                override fun set(`object`: LightView?, value: Int) {
                    if (value == canvasTranslate) {
                        return
                    }
                    canvasTranslate = value
                    invalidate()
                }

                override fun get(`object`: LightView?): Int {
                    return canvasTranslate
                }
            }
            val listener: Animator.AnimatorListener = object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    post { animator?.start() }
                }
            }
            val offsetW = abs(width * sin(30.0))
            animator = ObjectAnimator.ofInt(this, property, -shaderWidth, (width + offsetW).toInt()).apply {  // 旋转后补宽度
                interpolator = this@LightView.interpolator
                duration = this@LightView.duration

                addListener(listener)
                start()
            }
            animator?.startDelay = this@LightView.startDelay
        }
    }
    fun stopLight(){
        animator?.cancel()
        animator?.addListener(null)
        animator?.removeAllListeners()
        animator = null
    }

    fun setShader(shader: Shader?, shaderWidth: Int) {
        paint.shader = shader
        this.shaderWidth = shaderWidth
        startLight()
    }

    fun setInterpolator(interpolator: Interpolator) {
        this.interpolator = interpolator
        startLight()
    }

    fun setStartDelay(startDelay: Long) {
        this.startDelay = startDelay
        startLight()
    }

    fun setDuration(duration: Long) {
        this.duration = duration
        startLight()
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
       // startLight()
    }
    private val path = Path()
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (animator != null && animator!!.isRunning) { // 只有当动画正在运行时才绘制光效
            val count = canvas.save()
            try {
                path.reset()
                path.addRoundRect(
                    0f, 0f, width.toFloat(), height.toFloat(),
                    60f, 60f, Path.Direction.CW
                )
                canvas.clipPath(path)
                canvas.translate(canvasTranslate.toFloat(), 0f)
                canvas.rotate(30f)
                rect[0, -100, shaderWidth] = height + 250 // FIX 旋转后顶部留空
                canvas.drawRect(rect, paint)
            } finally {
                canvas.restoreToCount(count)
            }
        }
    }

}
